package p2pcdnsim.report;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import desmoj.core.simulator.Reportable;

public class SimReporter {
	
	public static final String NUMBER_OF_REQUESTS = "NUMBER_OF_REQUESTS";
	public static final String HIT_RATIO = "HIT_RATIO";
	public static final String MISS_RATIO = "MISS_RATIO";
	public static final String STARTUP_DELAY = "STARTUP_DELAY";
	public static final String PACKET_LOSS = "PACKET_LOSS";
	public static final String BANDWIDTH_CONSUMPTION = "BANDWIDTH_COMSUMPTION";
	
	private static SimReporter reporter = new SimReporter();
	
	public static SimReporter getInstance() {
		return reporter;
	}

	Map<String,Reportable> reportables;
	
	private SimReporter() {
		reportables = new HashMap<String, Reportable>();
	}
	
	public void registerReportable(String name, Reportable r) {
		reportables.put(name, r);
	}
	
	public void invokeReporter(String name, String methodName, Object ...args) {
		if(reportables.get(name) == null) return;
		try {
			Method method;
			if(args.length > 0) {
				Class<?>[] paramTypes = new Class[args.length];
				for(int i=0;i<args.length;i++) {
					Class<?> type = args[i].getClass();
					if(type.equals(Double.class)) type = Double.TYPE;
					if(type.equals(Integer.class)) type = Integer.TYPE;
					if(type.equals(Long.class)) type = Long.TYPE;
					paramTypes[i] = type;
				}
				method = reportables.get(name).getClass().getMethod(methodName, paramTypes);
			} else method = reportables.get(name).getClass().getMethod(methodName);
		method.invoke(reportables.get(name), args);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
	
}